﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;

namespace CQFQW
{
    /// <summary>
    ///  拽
    /// </summary>
    public class Zhuai
    {

        /// <summary>
        /// 是否继续
        /// </summary>
        public bool isStop = false;

        /// <summary>
        /// 获取文章是否结束了
        /// </summary>
        public bool zhuaiIdEndLoop = false;

        VoteCount voteCount;
        /// <summary>
        ///  锁定 获取地址
        /// </summary>
        GetUrl getUrl;
        public event SetTextCallback setText;

        public Zhuai(VoteCount voteCount, GetUrl getUrl)
        {
            this.voteCount = voteCount;
            this.getUrl = getUrl;
        }

        /// <summary>
        /// 开始 拽
        /// <script>alert('评价失败！');window.location='ShowContent.aspx?nid=005292';</script>0,0,0%,0%,0,0
        /// 0,0,0%,0%,0,0
        /// </summary>
        public void BeginZhuai()
        {
            string id = "";
            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            while (!isStop && !zhuaiIdEndLoop)
            {
                try
                {
                    id = getUrl.GetAdress();
                    if (id == null)
                    {
                        break;
                    }
                   ZhuaiZhauai(id, wc);
                }
                catch (Exception)
                {
                    voteCount.FailureCount++;
                    Thread.Sleep(2000);
                    if (setText != null)
                    {
                        this.setText("error:url="+id+"message:"+ voteCount.SuccessCount);
                    }
                }

            }
            lock (this)
            {
                if (zhuaiIdEndLoop == false)
                {
                    zhuaiIdEndLoop = true;
                    if (setText != null)
                    {
                        Thread.Sleep(5000);
                        this.setText("结束拽。 有效票数:" + voteCount.SuccessCount);
                    }
                }
            }

        }

        private void ZhuaiZhauai(string id,WebClient wc)
        {
            try
            {
               string url = string.Format("http://chaoju.csfqw.com/DiggAndTread.aspx?nid={0}&FlagName=Tread&articletitle", id);
                string result = wc.DownloadString(url);
                if (result.Contains("评价失败！"))
                {
                    if (setText != null)
                    {
                        setText(url + "评价失败！");
                    }
                }
                else
                {
                    voteCount.SuccessCount++;
                    if (setText != null)
                    {
                        this.setText(id+"成功:" + result);
                    }
                    string[] s = result.Split(',');
                    if (s!=null && s.Length==6)
                    {
                        if (Convert.ToInt32(s[1])<2)
                        {
                            ZhuaiZhauai(id, wc);
                        }
                    }

                }
            }
            catch (Exception)
            {
                voteCount.FailureCount++;
                Thread.Sleep(2000);
                if (setText != null)
                {
                    this.setText("error:url=" + id + "message:" + voteCount.SuccessCount);
                }
            }
            
        }

    }
}
